﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Configuration;
using System.IO;

namespace Incendios0_1
{
    /// <summary>
    /// Interaction logic for Configure.xaml
    /// </summary>
    public partial class Configure : Window
    {
        public Configure()
        {
            InitializeComponent();
            panelsSlider.Value = double.Parse(ConfigurationManager.AppSettings["NumCamerasDisplay"]);
            List<string> serialPortNames = new List<string>(System.IO.Ports.SerialPort.GetPortNames());
            serialPortNames.Insert(0, "None");
            camerasComboBox.ItemsSource = serialPortNames;
            smsComboBox.ItemsSource = serialPortNames;
            int selected = serialPortNames.IndexOf(ConfigurationManager.AppSettings["CameraSerialPort"]);
            if (selected < 1)
                camerasComboBox.SelectedIndex = 0;
            else
                camerasComboBox.SelectedIndex = selected;
            selected = serialPortNames.IndexOf(ConfigurationManager.AppSettings["SMSPort"]);
            if (selected < 1)
                smsComboBox.SelectedIndex = 0;
            else
                smsComboBox.SelectedIndex = selected;
            uriDirField.Text = ConfigurationManager.AppSettings["VideoDirectory"];
            if (uriDirField.Text == string.Empty)
                uriDirField.Text = Directory.GetCurrentDirectory();
        }

        private void exploreButton_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog dlg = new System.Windows.Forms.FolderBrowserDialog();
            if (uriDirField.Text == string.Empty)
                dlg.SelectedPath = Directory.GetCurrentDirectory();
            else
                dlg.SelectedPath = uriDirField.Text;
            System.Windows.Forms.DialogResult result = dlg.ShowDialog();
            // Process open file dialog box results
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                // Open document
                uriDirField.Text = dlg.SelectedPath;
            }
        }

        private void saveButton_Click(object sender, RoutedEventArgs e)
        {
            ConfigurationManager.AppSettings["NumCamerasDisplay"] = panelsSlider.Value.ToString();
            ConfigurationManager.AppSettings["CameraSerialPort"] = camerasComboBox.SelectedItem.ToString();
            ConfigurationManager.AppSettings["SMSPort"] = smsComboBox.SelectedItem.ToString();
            ConfigurationManager.AppSettings["VideoDirectory"] = uriDirField.Text;
            Microsoft.Windows.Controls.MessageBox.Show("La configuracion ha sido guardada", "Guardar", MessageBoxButton.OK, MessageBoxImage.Information);
        }

        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

    }
}
